<?php

namespace App\Admin\Controllers;

use App\Models\JxtConfig;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ConfigController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '网站信息';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new JxtConfig());

        $grid->column('id', __('Id'));
        $grid->column('address', __('Address'));
        $grid->column('mobile', __('Mobile'));
        $grid->column('tel', __('Tel'));
        $grid->column('url', __('Url'));
        $grid->column('wechat_official_account', __('Wechat official account'));
        $grid->column('wechat_appointment', __('Wechat appointment'));
        $grid->column('wechat_pay', __('Wechat pay'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(JxtConfig::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('address', __('Address'));
        $show->field('mobile', __('Mobile'));
        $show->field('tel', __('Tel'));
        $show->field('url', __('Url'));
        $show->field('wechat_official_account', __('Wechat official account'));
        $show->field('wechat_appointment', __('Wechat appointment'));
        $show->field('wechat_pay', __('Wechat pay'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new JxtConfig());
        $id = request()->route()->parameters()['config'] ?? 0;

        $form->text('address', __('地址'));
        $form->mobile('mobile', __('手机号'));
        $form->text('tel', __('电话'));
        $form->text('fax', __('传真'));
        $form->email('email', __('邮箱'));
        $form->url('url', __('网址'));
        $form->image('wechat_official_account', __('微信二维码'))->uniqueName()->move('config/images/' . date('Ymd'));
        $form->image('wechat_appointment', __('预约二维码'))->uniqueName()->move('config/images/' . date('Ymd'));
//        $form->image('wechat_pay', __('付款码'))->uniqueName()->move('config/images/' . date('Ymd'));
        $form->saved(function (Form $form) use ($id) {
            // 跳转页面
            admin_toastr('更新成功', 'success');
            return redirect('/admin/configs/' . $id . '/edit');
        });

        return $form;
    }
}
